package com.example.droideep.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.droideep.R;
import com.example.droideep.common.activities.SampleActivityBase;
import com.example.droideep.common.dummydata.Placeholder;
import com.example.droideep.fragment.animstudy.AnimationStudyFragment;
import com.example.droideep.fragment.animstudy.AnimatorSetFragment;
import com.example.droideep.fragment.animstudy.LayoutAnimationsFragment;
import com.example.droideep.fragment.animstudy.ParabolicMotionFragment;


public class MainActivity extends SampleActivityBase
        implements AnimationStudyFragment.OnParabolicMotionListener,
        AnimationStudyFragment.OnAnimatorSetListener,
        AnimationStudyFragment.OnLayoutAnimationsListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public void onParabolicMotion() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, ParabolicMotionFragment.newInstance())
                .addToBackStack(ParabolicMotionFragment.TAG)
                .commit();
    }

    @Override
    public void onAnimatorSet() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, AnimatorSetFragment.newInstance())
                .addToBackStack(AnimatorSetFragment.TAG)
                .commit();
    }

    @Override
    public void onLayoutAnimations() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, LayoutAnimationsFragment.newInstance())
                .addToBackStack(LayoutAnimationsFragment.TAG)
                .commit();
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends ListFragment {

        private Placeholder[] placeholders = Placeholder.PLACEHOLDERS;

        public PlaceholderFragment() {
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            setListAdapter(new PlaceholderAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, placeholders));
        }

        private class PlaceholderAdapter extends ArrayAdapter<Placeholder> {

            public PlaceholderAdapter(Context context, int resource, int textViewResourceId, Placeholder[] placeholders) {
                super(context, resource, textViewResourceId, placeholders);
            }

        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            getFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, placeholders[position].container)
                    .addToBackStack(placeholders[position].title)
                    .commit();
        }

    }

}
